<script setup>
import { ref } from 'vue'
//导入store
import useUserInfoStore from '@/stores/userInfo.js'
const userInfoStore = useUserInfoStore();
//...解构赋值为一个对象，对象userInfo的属性根据userInfoStore.info的属性来自动定义
const userInfo = ref({...userInfoStore.info})


//设置数据模型，老密码，新密码，确认密码
const passwords = ref({
    old_pwd:'',
    new_pwd:'',
    re_pwd:''
})

//定义表单校验规则，注意，每次只能规定一个参数，new_pwd,re_pwd:是不允许的
const rules = {
    new_pwd: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        {
            pattern: /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{2,10}$/,
            message: '密码必须包含大小写字母和数字的组合，不能使用特殊字符，长度在 2-10 之间',
            trigger: 'blur'
        }
    ],
    re_pwd: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        {
            pattern: /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{2,10}$/,
            message: '密码必须包含大小写字母和数字的组合，不能使用特殊字符，长度在 2-10 之间',
            trigger: 'blur'
        }
    ],
    old_pwd: [
        { required: true, message: '请输入老密码', trigger: 'blur' }
    ]
}


//修改密码
import {userPasswordUpdateService} from '@/api/user.js'
import {ElMessage} from 'element-plus'
//确认提示框
import {ElMessageBox} from 'element-plus'
//导入路由，更改成功后回到登陆页面
// import {useRouter} from 'vue-router'
// const router = useRouter()
const updateUserPassword = ()=>{
    //警告框警告一下
    ElMessageBox.confirm(
        '确定要修改密码吗？',
        '警告',
        {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
        }
    ).then(async ()=>{
        //调用接口，后端要求传入token
        let result = await userPasswordUpdateService(passwords.value);
        //判断一下，如果message不为空，则代表后端传递了error的message
        if(result.message){
            ElMessage.error(result.message)
        }else{
            ElMessage.success(result.data? result.data : '修改成功');
        }

            //清空token，以重新新登录
        userInfoStore.clearToken()
        //跳转到登录页面
        router.push('/login')
        



    }).catch(()=>{
        ElMessage({
            type: 'info',
            message: '已取消修改'
        });
    })
    
}
</script>
<template>
    <el-card class="page-container">
        <template #header>
            <div class="header">
                <span>更改密码</span>
            </div>
        </template>
        <el-row>
            <el-col :span="12">
                <el-form :model="passwords" :rules="rules" label-width="100px" size="large">
                    <el-form-item label="用户名">
                        <el-input v-model="userInfo.username" disabled></el-input>
                    </el-form-item>

                    <el-form-item label="老密码" prop="old_pwd">
                        <el-input v-model="passwords.old_pwd"></el-input>
                    </el-form-item>

                    <el-form-item label="新密码" prop="new_pwd">
                        <el-input v-model="passwords.new_pwd"></el-input>
                    </el-form-item>

                    <el-form-item label="确认密码" prop="re_pwd">
                        <el-input v-model="passwords.re_pwd"></el-input>
                    </el-form-item>

                    <el-form-item>
                        <el-button type="primary" @click="updateUserPassword">提交修改</el-button>
                    </el-form-item>
                </el-form>
            </el-col>
        </el-row>
    </el-card>
</template>